
////

package Clients;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import Middle.*;

/**
 * Implements the GUI for the Collection client.
 * @author  Michael Alexander Smith
 * @version 2.1
 */

class CollectionGUI
{

  private static final String COLLECT = "Collect";
  
  private static final int H = 300;       // Height of window pixels
  private static final int W = 400;       // Width  of window pixels

  private JLabel      action  = new JLabel();
  private JTextField  input   = new JTextField();
  private JTextArea   output  = new JTextArea();
  private JScrollPane scrollPane      = new JScrollPane();
  private JButton     pickedBtn= new JButton( COLLECT );
 
  private Transaction     transaction        = new Transaction();
  private OrderProcessing order     = null;

  public CollectionGUI(  RootPaneContainer rpc, MiddleFactory mf  )
  {
    try                                           // 
    {      
      order = mf.makeOrderProcessing();        // Process order
    } catch ( Exception e )
    {
      System.out.println("Exception: " + e.getMessage() );
    }
    Container container         = rpc.getContentPane();    // Content Pane
    Container rootWindow = (Container) rpc;         // Root Window
    container.setLayout(null);                             // No layout manager
    rootWindow.setSize( W, H );                     // Size of Window
    container.setBackground(Color.BLACK);

    Font f = new Font("Monospaced",Font.PLAIN,12);  // Font f is

    pickedBtn.setBounds( 16, 25+60*0, 80, 40 );    // Check Button
    pickedBtn.addActionListener( transaction );          // Listener
    pickedBtn.setIcon(new ImageIcon("src/images/ButtonTex/buttonTex.png")); 	//Changes the background image
    pickedBtn.setVerticalTextPosition(JButton.CENTER); 						// Sets the text to the middle
    pickedBtn.setHorizontalTextPosition(JButton.CENTER); 						//to avoid it bumping the image around
    pickedBtn.setBackground(Color.BLACK); 		//Sets the background to black
    pickedBtn.setBorder(null); 					//Removes the border around the button
    pickedBtn.setForeground(Color.WHITE); 		//Sets the font color to white
    pickedBtn.setContentAreaFilled(false); 		//Does not allow the background to change when clicked
    container.add( pickedBtn );                          //  Add to canvas

    action.setBounds( 110, 235 , 270, 20 );       // Message area
	action.setText("Please input the order number.");
	action.setForeground(Color.WHITE);
    container.add( action );                            //  Add to canvas

    input.setBounds( 110, 25, 270, 40 );         // Input Area
    input.setText("");                           // Blank
	input.setBackground(Color.BLACK);
	input.setForeground(Color.WHITE);
	input.setBorder(BorderFactory.createLineBorder(Color.green));
    container.add( input );                             //  Add to canvas

    scrollPane.setBounds( 110, 75, 270, 160 );          // Scrolling pane
    output.setText( "" );                        //  Blank
    output.setFont( f );                         //  Uses font
    output.setEditable(false);
	scrollPane.setBorder(null);
	output.setBackground(Color.BLACK);
	output.setForeground(Color.WHITE);
	output.setBorder(BorderFactory.createLineBorder(Color.green));
    container.add( scrollPane );                                //  Add to canvas
    scrollPane.getViewport().add( output );           //  In TextArea
    rootWindow.setVisible( true );                  // Make visible
    
  }
  
  class Transaction implements ActionListener       // Listener
  {
    public void actionPerformed( ActionEvent ae )   // Interaction
    {
      if ( order == null )
      {
        action.setText("No connection");
        return;                                     // No connection
      }
      String actionIs = ae.getActionCommand();      // Button
      try
      {
        if ( actionIs.equals( COLLECT ) )           // Button Collect
        {
        	int orderNo = 0;
            String on  = input.getText().trim(); // Product no.
            try
            {
              orderNo = Integer.parseInt(on);       // Convert
            }
            catch ( Exception err) {}

            boolean ok = 
             order.informOrderCollected(orderNo);
            if ( ok )
            {
              
              output.append("Collected order #" + orderNo + "\n");
            }
            else
            {
              action.setText("No such order");
            }
                     
        }
        input.requestFocus();                     // theInput has Focus
      }

      catch ( OrderException e )                     // Error
      {                                              //  Of course
        output.append( "Fail Order process:" +    //   Should not
                            e.getMessage() + "\n" ); //  happen
      }
    }
  }

}




